package com.TT.ticket.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) {
        test1();
    }


    /**
     * 抛出异常
     */
    public static void test1() {
        //队列的大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);


        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));

        //java.lang.IllegalStateException: Queue full 抛出异常
        //System.out.println(arrayBlockingQueue.add("d"));

        System.out.println(arrayBlockingQueue.element());  //查看队首元素
        System.out.println("=================================================");

        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());

        //Exception in thread "main" java.util.NoSuchElementException 抛出异常
        System.out.println(arrayBlockingQueue.remove());


    }


    /**
     * 不抛出异常
     */
    public static void test2() {

        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));


        //System.out.println(arrayBlockingQueue.offer("d")); //false 不抛出异常


        System.out.println(arrayBlockingQueue.peek());
        System.out.println("=========================================================");

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());


        //System.out.println(arrayBlockingQueue.poll());  null 不抛出异常


    }


    /**
     * 等待，阻塞（一直等待）
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);

        arrayBlockingQueue.put('a');
        arrayBlockingQueue.put('b');
        arrayBlockingQueue.put('c');


        // arrayBlockingQueue.put('d'); 队列没有位置了  一直阻塞


        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());


        // System.out.println(arrayBlockingQueue.take());  //没有元素了 一直阻塞
    }


    /**
     * 超时等待
     */
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);

        arrayBlockingQueue.offer("a",2, TimeUnit.SECONDS);
        arrayBlockingQueue.offer("b",2, TimeUnit.SECONDS);
        arrayBlockingQueue.offer("c",2, TimeUnit.SECONDS);


        // arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS);  等待2秒，超时退出

        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
        arrayBlockingQueue.poll(2,TimeUnit.SECONDS);


        //arrayBlockingQueue.poll(2,TimeUnit.SECONDS);
    }

}
